/**
 * @description 所有全局配置的状态管理，如无必要请勿修改
 */
import { matchTypesDetail } from '@/utils/constant'
import { PLAA0019 } from '@/api/client'
import {
  getBloodMatchTypes,
  getProducts,
  getBloodOutboundReasons,
  getBloodCodes,
  getBloodSources,
} from '@/api/basedata'
import { getOne as getSystemSet } from '@/api/systemSet'
import { convertArr2Obj, setArrayItem, getArrayItem } from '@/utils'

const state = () => ({
  matchTypesDetail,
  bloodMatchType: getArrayItem('bloodMatchType') || '[[1,3]]',
  bloodMatchTypes: getArrayItem('bloodMatchTypes', []),
  bloodOutboundReasons: getArrayItem('bloodOutboundReasons', []),
  bloodCodes: getArrayItem('bloodCodes', []),
  bloodSources: getArrayItem('bloodSources', []),
  products: getArrayItem('products', []),
  depts: {},
  bloodMatchTypeId: getArrayItem('bloodMatchTypeId'),
  bloodMatchTypePid: getArrayItem('bloodMatchTypePid', 1),
  systemSet: '',
})
const getters = {
  matchTypesDetail: (state) => state.matchTypesDetail,
  bloodMatchType: (state) => state.bloodMatchType,
  bloodMatchTypes: (state) => state.bloodMatchTypes,
  bloodOutboundReasons: (state) => state.bloodOutboundReasons,
  bloodCodes: (state) => state.bloodCodes,
  bloodSources: (state) => state.bloodSources,
  products: (state) => state.products,
  depts: (state) => state.depts,
  bloodMatchTypeId: (state) => state.bloodMatchTypeId,
  bloodMatchTypePid: (state) => state.bloodMatchTypePid,
  systemSet: (state) => state.systemSet,
}
const mutations = {
  setDepts(state, depts) {
    state.depts = depts
  },
  setBloodMatchType(state, bloodMatchType) {
    state.bloodMatchType = bloodMatchType
    setArrayItem('bloodMatchType', state.bloodMatchType)
    state.bloodMatchTypeId = bloodMatchType[0]
    setArrayItem('bloodMatchTypeId', state.bloodMatchTypeId)
    state.bloodMatchTypePid = bloodMatchType[0][0]
    setArrayItem('bloodMatchTypePid', state.bloodMatchTypePid)
  },
  setBloodMatchTypes(state, bloodMatchTypes) {
    state.bloodMatchTypes = bloodMatchTypes
  },
  setProducts(state, data) {
    state.products = data
  },
  setBloodCodes(state, data) {
    state.bloodCodes = data
    setArrayItem('bloodCodes', data)
  },
  setBloodSources(state, data) {
    state.bloodSources = data
    setArrayItem('bloodSources', data)
  },
  setBloodOutboundReasons(state, bloodOutboundReasons) {
    state.bloodOutboundReasons = bloodOutboundReasons
  },
  setSystemSet: (state, systemSet) => {
    state.systemSet = systemSet
  },
}
const actions = {
  async setBloodMatchTypes({ commit }, force = 0) {
    const { data } = await getBloodMatchTypes({ force })
    localStorage.setItem('bloodMatchTypes', JSON.stringify(data))
    commit('setBloodMatchTypes', data)
  },
  async setProducts({ commit }, force = 0) {
    const { data } = await getProducts({ force })
    localStorage.setItem('products', JSON.stringify(data))
    commit('setProducts', data)
  },
  async setBloodCodes({ commit }, force = 0) {
    const { data } = await getBloodCodes({ force })
    localStorage.setItem('products', JSON.stringify(data))
    commit('setBloodCodes', data)
  },
  async setBloodSources({ commit }, force = 0) {
    const { data } = await getBloodSources({ force })
    localStorage.setItem('bloodSources', JSON.stringify(data))
    commit('setBloodSources', data)
  },
  async setBloodOutboundReasons({ commit }, force = 0) {
    const { data } = await getBloodOutboundReasons({ force })
    localStorage.setItem('bloodOutboundReasons', JSON.stringify(data))
    commit('setBloodOutboundReasons', data)
  },
  async setDepts({ commit }) {
    let { data } = await PLAA0019()
    if (data.item) {
      data = data.item
    }
    commit('setDepts', convertArr2Obj(data, 'DEPT_CODE'))
  },
  setBloodMatchType({ commit }, bloodMatchType) {
    commit('setBloodMatchType', bloodMatchType)
  },
  getSystemSet({ commit }) {
    return new Promise((resolve, reject) => {
      getSystemSet(1)
        .then((response) => {
          const { data } = response
          commit('setSystemSet', data)
          resolve()
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}
export default { state, getters, mutations, actions }
